using System;
using Newtonsoft.Json;
using TEngine;
using UnityEngine;

#pragma warning disable CS0618

namespace GameLogic
{
    //用户注册类型
    public enum USER_REG_TYPE
    {
        NODE,
        //@weixin
        WX,
        //@游客
        [Obsolete("游客登录类型已废弃")]
        GUEST,
        //@phone
        PHONE,
        //@qq
        QQ,
        //@nbgame
        ACCOUNT
    }

    /// <summary>
    /// 性别
    /// </summary>
    public enum GENDER_TYPE
    {
        /// <summary>
        /// 男
        /// </summary>
        MALE,
        /// <summary>
        /// 女
        /// </summary>
        FEMALE
    }

    /// <summary>
    /// 用于玩家自身数据的存储
    /// </summary>
    public class UserVO : BindableVO
    {
        /// <summary>
        /// 从对象池租一个实例，需要归还，即调用Return()方法
        /// </summary>
        /// <returns></returns>
        public static UserVO Rent()
        {
            return MemoryPool.Acquire<UserVO>();
        }

        /// <summary>
        /// 归还对象到对象池
        /// </summary>
        public void Return()
        {
            MemoryPool.Release(this);
        }

        /** 玩家openID */
        public string openID = "";
        /** 玩家unionID */
        public string unionID = "";
        /** 玩家账号 */
        public string userName = "";
        /** 性别，服务器传过来的数据，1的话则为男 */
        public GENDER_TYPE userGender = GENDER_TYPE.MALE;
        /** 微信昵称 */
        public string wxName = "";
        /** 微信头像 */
        public string wxFace = "";
        /** 是否可以使用账号密码登录 */
        public bool isUserNameLogin = false;
        /** IP对应的地理位置 */
        public string district = "";

        /** 玩家用户ID */
        private string _userID = "";
        public string UserID
        {
            get => _userID;
            set => SetProperty(ref _userID, value);
        }

        /** 票据 */
        private string _authentic = "";
        public string Authentic
        {
            get => _authentic;
            set => SetProperty(ref _authentic, value);
        }
        
        /** 用户昵称 */
        private string _nickname = "";
        public string Nickname
        {
            get => _nickname;
            set => SetProperty(ref _nickname, value);
        }

        /** 玩家头像编号 */
        private int _uFace = 1;
        public int UFace
        {
            get => _uFace;
            set => SetProperty(ref _uFace, Mathf.Clamp(value, 1, 171));
        }

        /**​ 玩家自定义头像*/
        private string _uHead = "";
        public string UHead
        {
            get => _uHead;
            set => SetProperty(ref _uHead, value);
        }

        /**​ 钱币数量*/
        private int _money = 0;
        public int Money
        {
            get => _money;
            set => SetProperty(ref _money, Mathf.FloorToInt(value));
        }

        /** 红包余额数量*/
        private float _redPacketNum = 0f;
        public float RedPacketNum
        {
            get => _redPacketNum;
            set => SetProperty(ref _redPacketNum, (float)Math.Round(value, 2));
        }

        /**​ 元宝余额数量*/
        private int _yuanbaoNum = 0;
        public int YuanbaoNum
        {
            get => _yuanbaoNum;
            set => SetProperty(ref _yuanbaoNum, value);
        }

        /** 玩家身份证信息*/
        private string _IdCard = "";
        public string IdCard
        {
            get => _IdCard;
            set => SetProperty(ref _IdCard, value);
        }

        //玩家手机号
        private string _phoneNum = "";
        public string PhoneNum
        {
            get => _phoneNum;
            set => SetProperty(ref _phoneNum, value);
        }

        // 玩家实名认证信息
        private RealNameCertifyInfo _realNameCertifyInfo;
        public RealNameCertifyInfo RealNameCertifyInfo
        {
            get => _realNameCertifyInfo;
            set => SetProperty(ref _realNameCertifyInfo, value);
        }

        ///​**​ 玩家QQ信息*/
        private QQInfo _QQInfo;
        public QQInfo QQInfo
        {
            get => _QQInfo;
            set => SetProperty(ref _QQInfo, value);
        }

        ///​**​ 保险箱银子*/
        private int _bankMoney;
        public int BankMoney
        {
            get => _bankMoney;
            set => SetProperty(ref _bankMoney, Mathf.FloorToInt(value));
        }

        // 玩家注册方式
        private USER_REG_TYPE _regType = USER_REG_TYPE.NODE;

        public USER_REG_TYPE RegType
        {
            get => _regType;
        }

        public string RegTypeString
        {
            set
            {
                USER_REG_TYPE newType = USER_REG_TYPE.NODE;
                switch (value)
                {
                    case "@weixin":
                        newType = USER_REG_TYPE.WX;
                        break;
                    case "@游客":
                        newType = USER_REG_TYPE.GUEST;
                        break;
                    case "@phone":
                        newType = USER_REG_TYPE.PHONE;
                        break;
                    case "@qq":
                        newType = USER_REG_TYPE.QQ;
                        break;
                    case "@nbgame":
                        newType = USER_REG_TYPE.ACCOUNT;
                        break;
                    default:
                        {
                            throw new ArgumentException($"Unknown registration type: {value}");
                        }
                }
                if (_regType != newType)
                {
                    _regType = newType;
                }
            }
        }

        //地址
        private AddressInfo _address;
        public AddressInfo AddressInfo
        {
            get => _address;
            set => SetProperty(ref _address, value);
        }

        //玩家是否可以转换账号为大厅游戏账号 0: 不可以；1：可以
        private int _changeNbgameCount;
        public int ChangeNbgameCount
        {
            get => _changeNbgameCount;
            set => SetProperty(ref _changeNbgameCount, value);
        }

        private DressInfo _dressInfo;

        public DressInfo DressInfo
        {
            get => _dressInfo;
        }

        // 玩家装扮信息
        public string DressInfoString
        {
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    //解析装扮Json字符串
                    _dressInfo = JsonConvert.DeserializeObject<DressInfo>(value);
                }
            }
        }

        // 玩家微信信息
        private WechatInfo _wechatInfo;
        public WechatInfo WechatInfo
        {
            get => _wechatInfo;
            set => SetProperty(ref _wechatInfo, value);
        }

        public override void Clear()
        {
            base.Clear(); // 如果 BindableVO 也有需要清理的内容，别忘了调用

            openID = "";
            unionID = "";
            userName = "";
            userGender = GENDER_TYPE.MALE;
            wxName = "";
            wxFace = "";
            isUserNameLogin = false;
            district = "";
            _regType = USER_REG_TYPE.NODE;
            _userID = "";
            _authentic = "";
            _nickname = "";
            _uFace = 1;
            _uHead = "";
            _money = 0;
            _redPacketNum = 0f;
            _yuanbaoNum = 0;
            _IdCard = "";
            _phoneNum = "";
            _realNameCertifyInfo = null;
            _QQInfo = null;
            _dressInfo = null;
            _bankMoney = 0;
            _address = null;
            _changeNbgameCount = 0;
            _wechatInfo = null;
        }
    }

    /// <summary>
    /// 装扮信息类，当前包含时钟、头部、武器三种装扮类型
    /// </summary>
    [Serializable]
    public class DressInfo
    {
        public DressItem clock;
        public DressItem head;
        public DressItem weapon;
        //可以添加其他的装扮类型，格式为DressItem
    }

    /// <summary>
    /// 通用的单一装扮属性类
    /// </summary>
    [Serializable]
    public class DressItem
    {
        /// <summary>
        /// 失效时间
        /// </summary>
        public int effect_time;
        /// <summary>
        /// 装扮类型
        /// </summary>
        public string type;
        /// <summary>
        /// 装扮的模板ID
        /// </summary>
        public string template_id;
    }

    public class RealNameCertifyInfo
    {
        public string realname;
        public string idcard;
    }

    public class QQInfo
    {
        public string name;
        public string faceurl;
        public string date;
    }

    public class WechatInfo
    {
        public string wechatname;
        public string faceurl;
        public string date;
    }

    public class AddressInfo
    {
        public bool Init = false;
        public string UserName;
        public string UPhoneNum;
        public string Province;
        public string ProvinceId;
        public string City;
        public string CityId;
        public string County;
        public string CountyId;
        public string Town;
        public string TownId;
        public string AddressStr;

        public bool IsAddressComplete()
        {
            return !string.IsNullOrEmpty(UserName) &&
                   !string.IsNullOrEmpty(UPhoneNum) &&
                   !string.IsNullOrEmpty(AddressStr) &&
                   !string.IsNullOrEmpty(Province);
        }
    }
}